package com.zcb.agrimarket.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcb.agrimarket.mapper.ArrangeMapper;
import com.zcb.agrimarket.pojo.*;
import com.zcb.agrimarket.result.Result;
import com.zcb.agrimarket.service.ArrangeService;
import com.zcb.agrimarket.service.StudentApplyLogService;
import com.zcb.agrimarket.service.TeacherApplyLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/arrange")
@Api(tags = "排课相关接口")
@Slf4j
@CrossOrigin//$$$用于处理跨域问题
public class ArrangeController {

    @Autowired
    ArrangeService arrangeService;

    @Autowired
    StudentApplyLogService studentApplyLogService;

    @Autowired
    TeacherApplyLogService teacherApplyLogService;

    /**
     * 管理员查询学生的所有教室申请
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/studentApply")
    @ApiOperation("查询学生的所有教室申请")
    public Result<Page> getAll(Integer page, Integer pageSize){
        log.info("当前页码：{}",page);
        log.info("页面大小：{}",pageSize);
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<StudentApplyLog> queryWrapper = new LambdaQueryWrapper();

        //执行查询
        studentApplyLogService.page(pageInfo,queryWrapper);

        return Result.success(pageInfo);
    }

    /**
     * 管理员审批学生教室申请，同意并在在排课表中添加内容
     * @param studentApplyLog
     * @return
     */
    @PutMapping("/updataStudentApply")
    @ApiOperation("同意学生教室借用申请(如果同意state设置为1，否则设置为2)")
    public Result<String> updata(@RequestBody StudentApplyLog studentApplyLog){
        arrangeService.updataStudentApply(studentApplyLog);
        return  Result.success("完成申请审批！");
    }

    /**
     * 管理员查询教师的所有教室申请
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/teacherApply")
    @ApiOperation("查询教师的所有教室申请")
    public Result<Page> getAll1(Integer page, Integer pageSize){
        log.info("当前页码：{}",page);
        log.info("页面大小：{}",pageSize);
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<TeacherApplyLog> queryWrapper = new LambdaQueryWrapper();

        //执行查询
        teacherApplyLogService.page(pageInfo,queryWrapper);

        return Result.success(pageInfo);
    }

    /**
     * 查询哪些节次可以安排哪些教室满足教师的教室申请
     * @param teacherApplyLog
     * @return
     */
    @PostMapping("/checkTeacherApply")
    @ApiOperation("查询哪些节次可以安排哪些教室给教师")
    public Result<TeacherApplyData> checkTeacherApply(@RequestBody TeacherApplyLog teacherApplyLog){
        TeacherApplyData  teacherApplyLogArrayList = teacherApplyLogService.selectList(teacherApplyLog);
        return Result.success(teacherApplyLogArrayList);
    }

    /**
     * 进行排课操作
     */
    @PutMapping("managementForTeacher")
    @ApiOperation("为教室进行排课")
    public Result<String> managementForTeacher(@RequestBody TeacherApplyData teacherApplyData){
        if(teacherApplyData.getState()==1){
            arrangeService.managementForTeacher(teacherApplyData);
            teacherApplyLogService.updateById(teacherApplyData.getId(),1);
            return Result.success("完成排课！");
        }else if(teacherApplyData.getState()==2){
            teacherApplyLogService.updateById(teacherApplyData.getId(),2);
            return Result.success("已经驳回排课！");
        }else return Result.error("未知错误！");
    }

    @Autowired
    ArrangeMapper arrangeMapper;

    /**
     * 查询课表
     * @param semesterId
     * @param week
     * @return
     */
    @GetMapping("/show")
    @ApiOperation("查询课表")
    public Result<ArrayList<Arrange>> show(Long semesterId, Integer week){
        log.info("搜索学期：{}",semesterId);
        log.info("搜索周次：{}",week);

        //构造条件构造器
        LambdaQueryWrapper<Arrange> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.eq(Arrange::getSemesterId,semesterId)
                .eq(Arrange::getWeek,week);

        //执行查询
        ArrayList<Arrange> ArrangeList = (ArrayList<Arrange>) arrangeMapper.selectList(queryWrapper);

        return Result.success(ArrangeList);
    }


}
